package com.r_z.controller;

import com.r_z.entity.Users;
import com.r_z.service.UsersService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 梁俊
 * @date 2023/5/29
 * &@version 1.0
 */
@RestController
public class UsersController {

    @Autowired
    public UsersService usersService;


    /**
     * 验证登录
     * @param users 用户对象
     * @return user对象
     */
    @GetMapping("/checkLogin")
    @CrossOrigin
    public Users checkLogin(Users users){
        System.out.println("进来了登录的方法"+users.getUserName()+"\t"+users.getUserPwd());
        Users obj = usersService.checkLogin(users);
        return obj;
    }



    /**
     * 分页模糊查
     * @param userName 姓名
     * @param page  当前页
     * @param limit  每页显示数
     * @return  集合对象
     */
    @RequestMapping("/selectLikeUsers")
    @CrossOrigin
    public Map selectLikeUsers(String userName,int page,int limit){
        if (userName==null) {
            userName="";
           }
        Map map=new HashMap();
        map.put("userName","%"+userName+"%");
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        List<Users> users = usersService.selectLikeUsers(map);
        int count = usersService.selectUserNameCount("%" + userName + "%");
        Map map1=new HashMap();
        map1.put("code",0);
        map1.put("msg","");
        map1.put("count",count);
        map1.put("data",users);
        return map1;
    }





    /**
     * 删除用户
     * @param userId 传过来的用户id
     * @return
     */
    @DeleteMapping("/deleteDuoUsers")
    @CrossOrigin
    public int deleteDuoUsers(String userId){
        System.out.println("进来了删除"+userId);
        int i = usersService.deleteDuoUsers(userId);
        return i;
    }


    /**
     * 修改用户
     * @param users 传过来的用户对象
     * @return
     */
    @RequestMapping("/updateUsers")
    @CrossOrigin
    public int updateUsers(Users users){
        System.out.println(users+"信息");
        int i = usersService.updateUsers(users);
        return i;
    }


    /**
     * 根据id查询
     * @return
     */
    @RequestMapping("/selectIdUser")
    @CrossOrigin
    public Users selectIdUser(int userId){
        Users users = usersService.selectIdUser(userId);
        return users;
    }


    /**
     * 新增用户
     * @param users 传过来的用户对象
     * @return
     */
    @RequestMapping("/addUsers")
    @CrossOrigin
    public int addUsers(Users users){
        System.out.println("进来了新增");
        int i = usersService.addUsers(users);
        return i;
    }
}

